"""
Report.py
Copyright (C) otacon 2010 <orfeo18@gmail.com>

AdSense-Monitor is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

AdSense-Monitor is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
from datetime import date
import re

class Report:


    def __init__(self,start_time = None,end_time = None):
        if not start_time:
            self.start_time = date.today()
        else:
            self.start_time = start_time

        if not end_time:
            self.end_time = date.today()
        else:
            self.end_time = end_time

        self.report_rows = []


    def add_row(self, report_row):
        if not isinstance(report_row,ReportRow):
            raise ValueError("report_row parameter must be ReportRow")
        for row in self.report_rows:
            if row.get_day() == report_row.get_day():
                return False
        self.report_rows.append(report_row)


    def remove_row(self, report_row):
        if not isinstance(reportrow,ReportRow):
            raise ValueError("report_row parameter must be ReportRow")

        for row in self.report_rows:
            if row.get_day() == report_row.get_day():
                self.report_rows.remove(row)
                return True
        return False


    def get_row(self, year, month, day):
        report_time = date(year,month,day)
        for row in self.report_rows:
            if row.get_day() == report_time:
                return row
        return None

    def get_latest(self):
        if self.report_rows:
            return self.report_rows[0]
        else:
            return None


class ReportRow:


    def __init__(self):
        self.report_dict = {}
        self.report_dict['Earnings'] = 0
        self.report_dict['eCPM'] = 0
        self.report_dict['Impressions'] = 0
        self.report_dict['Clicks'] = 0
        self.report_dict['CTR'] = 0
        self.report_dict['Report Time'] = None
        

    def set_earnings(self,earnings):
        if not isinstance(earnings, (int, float)):
            raise ValueError("ecpm parameter must be int or float")
        self.report_dict['Earnings'] = earnings

    def get_earnings(self):
        return self.report_dict['Earnings']


    def set_ecpm(self,ecpm):
        if not isinstance(ecpm, (int, float)):
            raise ValueError("ecpm parameter must be int or float")
        self.report_dict['eCPM'] = ecpm

    def get_ecpm(self):
        return self.report_dict['eCPM']


    def set_impressions(self,impressions):
        if not isinstance(impressions, (int)):
            raise ValueError("impressions parameter must be int")
        self.report_dict['Impressions'] = impressions

    def get_impressions(self):
        return self.report_dict['Impressions']


    def set_clicks(self,clicks):
        if not isinstance(clicks, (int, float)):
            raise ValueError("clicks parameter must be int or float")
        self.report_dict['Clicks'] = clicks

    def get_clicks(self):
        return self.report_dict['Clicks']


    def set_ctr(self,ctr):
        if not isinstance(ctr, (int, float)):
            raise ValueError("ctr parameter must be int or float")
        self.report_dict['CTR'] = ctr

    def get_ctr(self):
        return self.report_dict['CTR']


    def set_day(self,year,month,day):
        report_time = date(year,month,day)
        self.report_dict['Report Time'] = report_time

    def get_day(self):
        return self.report_dict['Report Time']

    def get_differences(self,report_row):
        differences = []
        if report_row:
            if self.get_earnings() != report_row.get_earnings():
                differences.append('Earnings')
            if self.get_ecpm() != report_row.get_ecpm():
                differences.append('eCPM')
            if self.get_clicks() != report_row.get_clicks():
                differences.append('Clicks')
            if self.get_impressions() != report_row.get_impressions():
                differences.append('Impressions')
            if self.get_ctr() != report_row.get_ctr():
                differences.append('CTR')
            if self.get_day() != report_row.get_day():
                differences.append('Report Time')
        return differences
            
def parse_from_csv(csv_string):

    lines = csv_string.split('\n')
    del lines[0]
    del lines[-1]
    del lines[-1]
    del lines[-1]    
    report = Report()
    reg = re.compile('(....)-(..)-(..)\t(.*?)\t(.*?)\t"(.*?)%"\t"(.*?)"\t"(.*?)"')
    for line in lines:
        value = reg.findall( line )
        year = int(value[0][0])
        month = int(value[0][1])
        day = int(value[0][2])
        impressions = int(value[0][3])
        clicks = int(value[0][4])
        ctr = float(value[0][5].replace(',','.'))
        ecpm = float(value[0][6].replace(',','.'))
        earnings = float(value[0][7].replace(',','.'))

        report_row = ReportRow()        
        report_row.set_day(year,month,day)
        report_row.set_impressions(impressions)
        report_row.set_clicks(clicks)
        report_row.set_ctr(ctr)
        report_row.set_ecpm(ecpm)
        report_row.set_earnings(earnings)
        report.add_row(report_row)

    return report
